#include "EWAUIProgressBar.hpp"

namespace Eyer
{
    int EWAUIProgressBar::OnDraw()
    {
        if (type == BarType::VERTICAL) {
            // 推算 bar 的高度
            if(scrollSize - contentSize >= 0) {
                bar->SetMinHeight(wh.height());
                bar->marginTop = 0;
                marginStart = 0;
            }
            else {
                int barHeight = (scrollSize * 1.0 / contentSize) * wh.height();
                bar->SetMinHeight(barHeight);
            }
        }
        else if (type == BarType::HORIZONTAL) {
            // 推算 bar 的宽度
            if(scrollSize - contentSize >= 0) {
                bar->SetMinWidth(wh.width());
                bar->marginLeft = 0;
                marginStart = 0;
            }
            else {
                int barWidth = (scrollSize * 1.0 / contentSize) * wh.width();
                bar->SetMinWidth(barWidth);
            }
        }

        // 绘制背景
        RemoveAllUIGraph();
        EyerSmartPtr<EWAUIGraphRect> rect(new EWAUIGraphRect());
        rect->xy.SetX(0);
        rect->xy.SetY(0);
        rect->wh = wh;
        rect->background = EyerColor::WHITE;
        rect->background.a(0.3);
        AddUIGraph(rect);

        return 0;
    }

    bool EWAUIProgressBar::OnMouseMoveEvent      (const Vec2I & pos)
    {
        if (type == BarType::VERTICAL) {
            if (bar->IsDraging()) {
                int pressY = bar->pressPos.y();
                int barMarginTop = pos.y() - pressY;
                MarginStartChange(barMarginTop);
            }
        }
        else if (type == BarType::HORIZONTAL) {
            if (bar->IsDraging()) {
                int pressX = bar->pressPos.x();
                int barMarginLeft = pos.x() - pressX;
                MarginStartChange(barMarginLeft);
            }
        }

        return true;
    }

    int EWAUIProgressBar::MarginStartChange(int barMarginStart)
    {
        if (type == BarType::VERTICAL) {
            bar->marginTop = barMarginStart;
            if (bar->marginTop <= 0) {
                bar->marginTop = 0;
            }
            if (bar->marginTop >= (wh.height() - bar->wh.height())) {
                bar->marginTop = (wh.height() - bar->wh.height());
            }

            if (wh.height() - bar->wh.height() > 0) {
                float progress = bar->marginTop * 1.0 / (wh.height() - bar->wh.height());
                marginStart = (scrollSize - contentSize) * progress;
            } else {
                marginStart = 0;
            }
        }
        else if(type == BarType::HORIZONTAL){
            bar->marginLeft = barMarginStart;
            if (bar->marginLeft <= 0) {
                bar->marginLeft = 0;
            }
            if (bar->marginLeft >= (wh.width() - bar->wh.width())) {
                bar->marginLeft = (wh.width() - bar->wh.width());
            }

            if (wh.width() - bar->wh.width() > 0) {
                float progress = bar->marginLeft * 1.0 / (wh.width() - bar->wh.width());
                marginStart = (scrollSize - contentSize) * progress;
            } else {
                marginStart = 0;
            }
        }
        return 0;
    }

    EWAUIProgressBar::EWAUIProgressBar(BarType _type, EWAUILayoutParams width, EWAUILayoutParams height)
        : EWAUIWidget(width, height)
        , bar(new EWAUIFrame(EWAUILayoutParams::WRAP_CONTENT, EWAUILayoutParams::WRAP_CONTENT))
        , type(_type)
    {
        if(type == BarType::VERTICAL){
            SetWidgetName("EWAUIProgressBar_VERTICAL");
            bar->SetWidgetName("EWAUIProgressBar_VERTICAL_BAR");

            SetMinWH(barWidth, 0);
            bar->SetMinWH(barWidth, barHeight);
            bar->rounding   = barWidth / 2;
            bar->bgColor    = EyerColor::BLACK;
            bar->bgColor.a(0.4);
            AddWidget(bar);
        }
        else if(type == BarType::HORIZONTAL){
            SetWidgetName("EWAUIProgressBar_HORIZONTAL");
            bar->SetWidgetName("EWAUIProgressBar_HORIZONTAL_BAR");
            SetMinWH(0, barHeight);
            bar->SetMinWH(barWidth, barHeight);
            bar->rounding   = barHeight / 2;
            bar->bgColor    = EyerColor::BLACK;
            bar->bgColor.a(0.4);
            AddWidget(bar);
        }
    }

    EWAUIProgressBar::~EWAUIProgressBar()
    {

    }

    int EWAUIProgressBar::SetSize(int _contentSize, int _scrollSize)
    {
        contentSize = _contentSize;
        scrollSize = _scrollSize;
        return 0;
    }

    int EWAUIProgressBar::GetMaginStart()
    {
        return marginStart;
    }
}